1912K - Kim's Quest - CodeForces Solution


bitmasks combinatorics dp *1800

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define all(x) (x).begin(),(x).end()
 
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

const ll MAX=2e5+10,P=998244353;
const ll INF=0x3f3f3f3f,oo=0x3f3f3f3f3f3f3f3f;

ll n;
ll dp[MAX][4];
bool a[MAX];
ll cnt[4]={0,0,0,0};
ll sgl[2]={0,0};

int main() {
	speed;
	cin>>n;
	for (int i=1;i<=n;i++) {
		ll x;
		cin>>x;
		a[i]=(x&1);
	}
	memset(dp,0,sizeof(dp));
	for (int i=1;i<=n;i++) {
		if (a[i]) {
			dp[i][0]=dp[i-1][0];
			dp[i][1]=dp[i-1][1]+dp[i-1][2]+cnt[2];
			dp[i][2]=dp[i-1][2];
			dp[i][3]=dp[i-1][3]+dp[i-1][1]+cnt[1];
		} else {
			dp[i][0]=dp[i-1][0]+dp[i-1][0]+cnt[0];
			dp[i][1]=dp[i-1][1];
			dp[i][2]=dp[i-1][2]+dp[i-1][3]+cnt[3];
			dp[i][3]=dp[i-1][3];
		}
		for (int k=0;k<4;k++) {
			dp[i][k]%=P;
//			cout<<dp[i][k]<<" ";
		}
//		cout<<"\n";
		cnt[a[i]]+=sgl[0];
		cnt[2+a[i]]+=sgl[1];
		sgl[a[i]]++;
	}
	cout<<(dp[n][0]+dp[n][1]+dp[n][2]+dp[n][3])%P<<"\n";
	return 0;
}


Comments

Submit
0 Comments
More Questions

136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction